package com.innovation.service;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.innovation.config.ConfigHelper;
import com.innovation.config.GlobalConfig;
import com.innovation.config.SqlMapperConfig;
import com.innovation.util.config.PackageUtil;
import com.innovation.util.config.SimpleObjectOutputStream;

public class ClientService {
	/**
	 * 传输资源库数据
	 * 
	 * @return
	 * @throws Exception
	 */
	public static ByteArrayOutputStream transferResourceData() throws Exception {
		// 需要传输的表数据字段
		List<String> transferFields = new ArrayList<String>();
		initTransferFields(transferFields);

		List<String> tmpList = new ArrayList<String>();
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		SimpleObjectOutputStream outputStream = new SimpleObjectOutputStream(buf);
		int count = 0;

		for (String fieldName : transferFields) {
			Field field = ConfigHelper.class.getDeclaredField(fieldName);
			field.setAccessible(true);
			Object fieldValue = field.get(null);
			if (fieldValue instanceof Map<?, ?>) {
				Map<?, ?> map = (Map<?, ?>) fieldValue;
				if (map.size() > 0) {
					// List<?> keyList = new ArrayList(map.keySet());
					List<?> valueList = new ArrayList(map.values());
					if (valueList.get(0) instanceof Map) {
						for (Object obj : valueList) {
							Map<?, ?> subMap = (Map<?, ?>) obj;
							List<?> list = new ArrayList(subMap.values());
							outputObject(list, outputStream, tmpList, field);
						}
					} else {
						outputObject(valueList, outputStream, tmpList, field);
						// String simpleName =
						// valueList.get(0).getClass().getSimpleName();
						//
						// System.out.println("table:"+
						// simpleName+" transfering "+
						// field.getName()+"......");
						// outputStream.writeString(simpleName);
						// outputStream.writeInt(valueList.size());
						// for (Object object : valueList) {
						// outputStream.writeBean(object);
						// }
						// tmpList.add(simpleName);
					}
				}
			}
		}
		// Field[] fields = ConfigHelper.class.getDeclaredFields();
		//
		// for (Field field : fields) {
		// if(!transferFields.contains(field.getName()))
		// continue;
		// if(field.getName().equals("configSkillEffectMap")){
		// System.out.println(field);
		// }
		//
		// }
		List<String> resultList = new ArrayList<String>();
//		List<String> classes = PackageUtil.getClassName(ConfigPlayerLevel.class.getPackage().getName(), false);
//		for (int i = 0; i < classes.size(); i++) {
//			String className = classes.get(i);
//			if (className.endsWith("Example")) {
//				classes.remove(i);
//				i--;
//				continue;
//			}
//
//			Class<?> clazz = Class.forName(className);
//			if (!clazz.getSimpleName().startsWith("Config")) {
//				classes.remove(i);
//				i--;
//				continue;
//			}
//
//			resultList.add(clazz.getSimpleName());
//		}
		for (String clazz : resultList) {
			if (!tmpList.contains(clazz)) {
				System.out.println(clazz);
			}
		}
		outputStream.flush();
		outputStream.close();
		return buf;
	}

	/**
	 * 初始化需要传输的字段,ConfigHelper中的字段属性
	 * 
	 * @param transferFields
	 */
	private static void initTransferFields(List<String> transferFields) {
		transferFields.add("configPlayerLevelMap");
		transferFields.add("configHeroMap");
		transferFields.add("configHeroLevelMap");
		transferFields.add("configHeroQualityMap");
		transferFields.add("configHeroPropMap");
		transferFields.add("configBuildingTileMap");
		transferFields.add("configMapMap");
		transferFields.add("configItemTypeMap");
		transferFields.add("configItemMap");
		transferFields.add("configMonsterMap");
		transferFields.add("configMonsterPropMap");
		transferFields.add("configRangeTypeMap");
		transferFields.add("configSkillMap");
		transferFields.add("configBoxMap");
		transferFields.add("configGoodsMap");
		transferFields.add("configTalentTypeMap");
		transferFields.add("configTalentMap");
		transferFields.add("configHeroLotteryMap");
		transferFields.add("configRewardMap");
		transferFields.add("configBulletinMap");
		transferFields.add("configMissionMap");
		transferFields.add("configMsgMap");
		transferFields.add("configItemSkillMap");
		transferFields.add("configPvpMap");
		transferFields.add("configPvpMapMap");
	}

	private static void outputObject(List<?> valueList, SimpleObjectOutputStream outputStream, List<String> tmpList, Field field) throws Exception {
		String simpleName = valueList.get(0).getClass().getSimpleName();

		System.out.println("table:" + simpleName + "----> transfering " + field.getName() + "......");
		outputStream.writeString(simpleName);
		outputStream.writeInt(valueList.size());
		for (Object object : valueList) {
			outputStream.writeBean(object);
		}
		tmpList.add(simpleName);
	}

	public static void main(String[] args) {
		// 初始化配置
		try {
			GlobalConfig.init();

			// 资源信息初始化
			SqlMapperConfig.initResSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 初始化配置
		ConfigHelper.init();

		String path = "D:\\project\\tanchishe\\Snake-Client\\Snake\\res\\snake_resource.ccbi";
		if(args.length >= 1){
			path = args[0];
		}
		System.out.println(path);
		
		try {
			File file = new File(path);
			if (file.exists()) {
				file.delete();
			}
			FileOutputStream outputStream = new FileOutputStream(file);
			ByteArrayOutputStream buf = transferResourceData();
			DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
			buf.writeTo(dataOutputStream);
			buf.flush();
			outputStream.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
